/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.repository;

import cz.mzk.oai.metadataRepository.repository.MetadataCollection;
import cz.mzk.oai.metadataRepository.metadata.DateStamp;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class Item {

    private String id;
    private MetadataCollection collection;
    private boolean inMarc;
    private boolean inEse;
    private boolean inDc;
    private static String PROPERTIES_SEPARATOR = " ";
    private DateStamp dateStamp;

    /**
     * @param id identifier
     * @param propertyString String created by Item.toString()
     */
    public Item(String id, MetadataCollection collection, String propertyString) {
        this(id, collection, false, false, false, null);
        String encodedSeparator = MetadataCollection.PROPERTIES_SEPARATOR;
        String[] tokens = propertyString.split(encodedSeparator);
        this.inMarc = Boolean.parseBoolean(tokens[0]);
        this.inEse = Boolean.parseBoolean(tokens[1]);
        this.inDc = Boolean.parseBoolean(tokens[2]);
        this.dateStamp = parseDateStamp(tokens[3]);
    }

    public Item(String id, MetadataCollection collection, boolean inMarc, boolean inEse, boolean inDc, DateStamp dateStamp) {
        this.id = id;
        this.collection = collection;
        this.inMarc = inMarc;
        this.inEse = inEse;
        this.inDc = inDc;
        this.dateStamp = dateStamp;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @return the collection
     */
    public MetadataCollection getCollection() {
        return collection;
    }

    /**
     * @return the inMarc
     */
    public boolean isInMarc() {
        return inMarc;
    }

    /**
     * @return the inEse
     */
    public boolean isInEse() {
        return inEse;
    }

    /**
     * @return the inDc
     */
    public boolean isInDc() {
        return inDc;
    }

    public String toProperty() {
        StringBuilder builder = new StringBuilder();
        builder.append(inMarc);
        builder.append(PROPERTIES_SEPARATOR);
        builder.append(inEse);
        builder.append(PROPERTIES_SEPARATOR);
        builder.append(inDc);
        builder.append(PROPERTIES_SEPARATOR);
        builder.append(getDateStamp());
        return builder.toString();
    }

    private DateStamp parseDateStamp(String string) {
        DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
        return new DateStamp(string);
    }

    /**
     * @return the dateStamp
     */
    public DateStamp getDateStamp() {
        return dateStamp;
    }

    public String getDateStampStr() {
        return dateStamp.toString();
    }

    /**
     * @param dateStamp the dateStamp to set
     */
    public void setDateStamp(DateStamp dateStamp) {
        this.dateStamp = dateStamp;
    }

    /**
     * @param inMarc the inMarc to set
     */
    public void setInMarc(boolean inMarc) {
        this.inMarc = inMarc;
    }

    /**
     * @param inEse the inEse to set
     */
    public void setInEse(boolean inEse) {
        this.inEse = inEse;
    }

    /**
     * @param inDc the inDc to set
     */
    public void setInDc(boolean inDc) {
        this.inDc = inDc;
    }
}
